﻿using DesignMode.BuilderMode.Examples.Component.Impl;
using DesignMode.BuilderMode.Examples.Component;
using DesignMode.BuilderMode.Examples;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DesignMode.BuilderMode;
using builderDesignMode.Decoretor;
using builderDesignMode.Proxy;
using builderDesignMode.Configuration.Provider;

namespace DesignMode
{
    public class BuilderModeOne
    {
        public BuilderModeOne()
        {

        }


        /// <summary>
        /// 建造者模式
        /// 1、构造对象：有多个属性的对象
        /// </summary>
        public void Run()
        {

            #region 配置建造者
            {
                //while (true) 
                //{
                //    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
                //    configurationBuilder.AddJsonFile("jsconfig.json",true,true);
                //    IConfiguration xx = configurationBuilder.Build();
                //    var x = xx.GetSection("AllowedHosts");
                //    Console.WriteLine(x.Value);
                //    Thread.Sleep(2000);
                //}

                JsonConfigurationProvider jsonConfigurationProvider = new JsonConfigurationProvider();
                jsonConfigurationProvider.AddJsonConfigurationFile();

                ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
                configurationBuilder.AddJsonFile("jsconfig.json", true, true);
                IConfiguration xx = configurationBuilder.Build();
                var x = xx.GetSection("AllowedHosts");
            }
            #endregion

            #region 代理模式 扩展原有方法的功能 结构行设计模式 改变原有类的结构  Aop动态代理模式
            {
                //相同点
                //都是给方法添加功能
                //都是结构型设计模式

                //不同点
                //装饰器：在于扩展功能，不替换原有类功能，不控制原有类
                //代理模式：在于扩展功能，会代替原有功能，完全控制原有类，项目只能使用代理类，不能使用原有类

                //何时用代理
                //新功能 ，是必须，那么就是用代理

                //何时用装饰
                //新功能，非必须（可以用，可以不用），那么就使用装饰器

                //Aop 动态代理模式
                IFileUpload fileUpload = new FileUpload();
                ProxyFileUpload proxyFileUpload = new ProxyFileUpload(fileUpload);
                //proxyFileUpload.fileUpload = fileUpload;
                proxyFileUpload.Upload();
            }
            #endregion

            #region 建造者模式 1、构造对象：有多个属性的对象
            {
                //建造者模式 复用对象构造属性
                //不同对象构造需求，遵守开闭原则

                //如果需求增多，建造者数量变多
                //增加代码复杂性，导致理解困难
                //工厂模式，解决建造者问题

                //使用时机（条件）
                //对象有多属性的时候
                //属性数量稳定 不容易发生变化


                ////1、原始构造对象 new()
                //IFrame frame = new AlloyFrame();
                //ISeat seat = new DermisSeat();
                //ITire tire = new SolidTire();
                //Bike bike = new Bike(frame, seat, tire);

                ////2、属性构造
                //IFrame frame = new AlloyFrame();
                //ISeat seat = new DermisSeat();
                //ITire tire = new SolidTire();

                //Bike bike = new Bike();
                //bike.frame = frame;
                //bike.seat = seat;
                //bike.tire = tire;

                //// 3、创建车建造者
                //IBikeBuilder bikeBuilder = new BikeBuilder();
                //bikeBuilder.BuildFrame();
                //bikeBuilder.BuildSeat();
                //bikeBuilder.BuildTire();

                //Bike bike1 = bikeBuilder.Build();

                // 3.1、黄金自行车
                IBikeBuilder goalBikeBuilder = new GoalBikeCoppyBuilder();
                goalBikeBuilder.BuildFrame();
                goalBikeBuilder.BuildSeat();
                goalBikeBuilder.BuildTire();

                Bike bike2 = goalBikeBuilder.Build();

                // 3.2、黄金自行车 属性建造者模式
                IGoalBikeBuilder builder = new GoalBikeBuilder();
                Bike bike = builder.Build();
            }
            #endregion



            #region 装饰器模式 扩展原有方法的功能时 结构型设计模式 改变原有类的结构  Aop动态代理模式
            {
                //原有方法扩展功能违背开闭原则
                //IPayCallBack payCallBack = new PayCallBack();

                ////装饰器模式 遵守开闭原则
                //SmSDecoretorCallBack smSDecoretorCallBack = new SmSDecoretorCallBack();
                //smSDecoretorCallBack.PayCallBack = new PayCallBack();
                //smSDecoretorCallBack.CallBack();

                //为方法扩展多个方法
                //EmailDecoretorCallBack emailDecoretorCallBack = new EmailDecoretorCallBack();
                //emailDecoretorCallBack.PayCallBack = new PayCallBack();
                //emailDecoretorCallBack.CallBack();

                //显示邮件然后短信
                EmailDecoretorCallBack emailDecoretorCallBack1 = new EmailDecoretorCallBack();
                emailDecoretorCallBack1.PayCallBack = new PayCallBack();

                SmSDecoretorCallBack smSDecoretorCallBack1 = new SmSDecoretorCallBack();
                smSDecoretorCallBack1.PayCallBack = emailDecoretorCallBack1;
                smSDecoretorCallBack1.CallBack();
            }
            #endregion
        }
    }
}
